<?php
/**
 * Mista Model for Ohlasky Component
 *
 * @license		GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');

/**
 * mista  Model
 */
class OhlaskysModelSablony extends JModel {
/**
 * Constructor that retrieves the ID from the request
 *
 * @access	public
 * @return	void
 */
    function __construct() {
        parent::__construct();

        $array = JRequest::getVar('cid',  0, '', 'array');
        $this->setId((int)$array[0]);
    }

    /**
     * Method to set the mista identifier
     *
     * @access	public
     * @param	int mista identifier
     * @return	void
     */
    function setId($id) {
    // Set id and wipe data
        $this->_id		= $id;
        $this->_data	= null;
    }

    /**
     * Method to get a mista
     * @return object with data
     */
    function &getData() {
    // Load the data
        if (empty( $this->_data )) {
            $query = ' SELECT * FROM #__sablony '.
                '  WHERE id = '.$this->_id;
            $this->_db->setQuery( $query );
            $this->_data = $this->_db->loadObject();
        }
        if (!$this->_data) {
            $this->_data = new stdClass();
            $this->_data->id = 0;
            $this->_data->nazev = null;
        }
        return $this->_data;
    }

    /**
     * Method to store a record
     *
     * @access	public
     * @return	boolean	True on success
     */
    function store() {
        $row =& $this->getTable();

        $data = JRequest::get( 'post' );

        // Bind the form fields to the mista table
        if (!$row->bind($data)) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }

        // Make sure the mista record is valid
        if (!$row->check()) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }

        // Store the web link table to the database
        if (!$row->store()) {
            $this->setError( $row->getError() );
            return false;
        }

        return true;
    }

    /**
     * Method to delete record(s)
     *
     * @access	public
     * @return	boolean	True on success
     */
    function delete() {
        $cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );

        $row =& $this->getTable();

        if (count( $cids )) {
            foreach($cids as $cid) {
            //zjistime, zda ma sablona udalosti
            //a vymazeme je
                $uspech_bool = $this->vymazUdalostiSablony($cid);
                if (!$row->delete( $cid )) {
                    $this->setError( $row->getErrorMsg() );
                    return false;
                }
            }
        }
        return true;
    }

    //vraci nejvyssi id (integer)
    //nebo nulu
    function getNejvyssiId() {
        $id_int = 0;
        $query = ' SELECT * FROM #__sablony ORDER BY #__sablony.id DESC LIMIT 1';
        $this->_db->setQuery( $query );
        $nejvysSablona_obj = $this->_db->loadObject();
        if (is_object($nejvysSablona_obj)) {
            $id_int = $nejvysSablona_obj->id;
        }
        return $id_int;
    }

    function neniStejnojmenna(&$nazev, &$id = 0) {
        $nazevOk_bool = true;
        $db =& JFactory::getDBO();
        if (!empty($nazev) && (empty($id))) {
            $query = 'SELECT * FROM #__sablony WHERE #__sablony.nazev ="'.$nazev.'"';
            $db->setQuery( $query );
            $stejnojmennaSabl_obj = $db->loadObject();
            if (is_object($stejnojmennaSabl_obj)) {
                $nazevOk_bool = false;
            }
        }
        if (!empty($nazev) && (!empty($id))) {
            $query = 'SELECT * FROM #__sablony WHERE #__sablony.nazev ="'.$nazev.'" AND NOT (#__sablony.id = '.$id.')';
            $db->setQuery( $query );
            $stejnojmennaSabl_obj = $db->loadObject();
            if (is_object($stejnojmennaSabl_obj)) {
                $nazevOk_bool = false;
            }
        }
        return $nazevOk_bool;
    }

    function vymazUdalostiSablony(&$cid) {
        $uspech_bool = false;
        if (!empty($cid)) {
            $db =& JFactory::getDBO();
            $row =& $this->getTable('udalost_sabl');
            $query = 'SELECT * FROM #__udalost_sabl WHERE #__udalost_sabl.id ='.$cid;
            $db->setQuery( $query );
            $udalosti_obj_list = $db->loadObjectList();
            if (!empty($udalosti_obj_list)) {
                foreach($udalosti_obj_list as $udalost_obj) {
                    $udalost_id = $udalost_obj->id;
                    $row->delete( $udalost_id );
                    $uspech_bool = true;
                }
            }
        }
        return $uspech_bool;
    }

}

